package com.hongkzh.www.user.service;

import com.google.common.collect.Lists;
import com.hongkzh.www.adv.service.BaseService;
import com.hongkzh.www.conmon.IdGen;
import com.hongkzh.www.conmon.RedisTool;
import com.hongkzh.www.conmon.RongCloudTool;
import com.hongkzh.www.conmon.StaticPublic;
import com.jfinal.kit.Kv;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;

import java.util.List;
import java.util.Random;

/**
 * @author martins
 */
public class UserService extends BaseService {

    public Record findDayIntegral(String id){
       SqlPara sqlPara = Db.getSqlPara("user.findDayIntegral", id);
       Record record=Db.findFirst(sqlPara);
       if(record==null){
           record=new Record();
           record.set("integral",0);
       }
       if(StrKit.isBlank(record.getStr("integral")) ){
           record.set("integral",0);
       }
       return record;

    }

    public Page<Record> followList(String id, int pageNumber) {
        SqlPara sqlPara = Db.getSqlPara("user.followList",id);
        Page<Record> records=Db.paginate(pageNumber,StaticPublic.PAGESIZE,sqlPara);

        for (Record r:records.getList()) {
            doFilePath(r);
        }
        return records;
    }

    public Page<Record> historyList(String id, int pageNumber) {
        SqlPara sqlPara = Db.getSqlPara("user.historyList",id);
        Page<Record> records=Db.paginate(pageNumber, StaticPublic.PAGESIZE,sqlPara);

        for (Record r:records.getList()) {
            doFilePath(r);
        }
        return records;
    }

    public void followDelete(String uid, String followUserId) {

        String sqlPara = Db.getSql("user.followDelete");

        Db.update(sqlPara,uid,followUserId);
    }

    public Record followAdd(String followUserId, String uid) {

        SqlPara sql=Db.getSqlPara("user.getfriend",followUserId,uid);
        List<Record> friends= Db.find(sql);
         //不是好友
         if(friends==null||friends.size()==0) {
             sql = Db.getSqlPara("user.getfollow", followUserId, uid);
             List<Record> follows = Db.find(sql);
             //相互关注 变成好友
             if (null != follows && follows.size() > 0) {
                 String id = IdGen.uuid();
                 sql = Db.getSqlPara("user.friendAdd", id, followUserId, uid);
                 Db.update(sql);
             }
         }


        String sqlPara = Db.getSql("user.followAdd");
        String id= IdGen.uuid();
        Db.update(sqlPara,id,uid,followUserId);
        Record record=new Record();
        record.set("id",id);
        return record;
    }

    public List<Record> historyAdd(String advId, String uid) {

        String sqlPara = Db.getSql("user.historyAdd");
        String id= IdGen.uuid();
        SqlPara advPara = Db.getSqlPara("adv.getAllAdvById",advId);
         Record r=Db.findFirst(advPara);
         if(r!=null) {
             String type=r.getStr("type");
             Db.update(sqlPara, id, uid, r.getStr("imgSrc"), r.getStr("imgLinks"), r.getStr("imgRank"), r.getStr("model")
                     , type, r.getStr("title"), advId);

            if("1".equals(type)){
                //自媒体


            }
             else if("2".equals(type)){
                 //企业广告
             }
             else if("3".equals(type)){
                 //城市广告
             }
            else  if("4".equals(type)){
                 //传统文化
             }
         }
        return null;
    }

    public Record getUserInfo(String loginUid, String uid) {
        SqlPara advPara = Db.getSqlPara("user.getUserInfo",loginUid,uid);
        Record r=Db.findFirst(advPara);
       doFilePath(r);

        r.set("followState","0");
        if(StrKit.notBlank(r.getStr("followId"))){
            r.set("followState","1");
        }
        return r;
    }

    public Page<Record> getUserAdvList(String uid, int pageNumber, String sortId, String sortValue) {
        if("create_date".equals(sortId)) {sortId=" a.create_date ";}
        else if("rank".equals(sortId)){ sortId=" a.img_rank ";}
        Kv cond=Kv.by("uid",uid).set("order", " order by "+ sortId+" "+sortValue);
        SqlPara sqlPara = Db.getSqlPara("user.getAllAdvByUserId",cond);
        Page<Record> records=Db.paginate(pageNumber,StaticPublic.PAGESIZE,sqlPara);

        for (Record r:records.getList()) {
            doFilePath(r);

        }
        return records;

    }

    public Page<Record> getAllAlbumByUserId(String uid, int pageNumber, String sortId, String sortValue) {

        Kv cond=Kv.by("uid",uid).set("order", " order by "+ sortId+" "+sortValue);
        SqlPara sqlPara = Db.getSqlPara("user.getAllAlbumByUserId",cond);
        Page<Record> records=Db.paginate(pageNumber,StaticPublic.PAGESIZE,sqlPara);
        List<Record> details= Lists.newArrayList();
        for (Record r:records.getList()) {
            if(StrKit.notBlank(r.getStr("coverPhoto"))){
                r.set("coverPhoto", PropKit.get("fileServer")+ r.get("coverPhoto"));}
            details.clear();
            details=getAllAlbumContentByAlbumId(r.getStr("id"));
            r.set("details",details);
        }
        return records;
    }

    public List<Record> getAllAlbumContentByAlbumId(String albumId) {


        SqlPara sqlPara = Db.getSqlPara("user.getAllAlbumContentByAlbumId",albumId);
        List<Record> records=Db.find(sqlPara);

        for (Record r:records) {
           doFilePath(r);

        }
        return records;
    }

    public Page<Record> getAllProductByUserId(String uid, int pageNumber, String sortId, String sortValue) {
        Kv cond=Kv.by("uid",uid).set("order", " order by "+ sortId+" "+sortValue);
        SqlPara sqlPara = Db.getSqlPara("user.getAllProductByUserId",cond);
        Page<Record> records=Db.paginate(pageNumber,StaticPublic.PAGESIZE,sqlPara);

        for (Record r:records.getList()) {
            doFilePath(r);

        }
        return records;
    }

    public void historyDelete(String id) {
        SqlPara sqlPara = Db.getSqlPara("user.historyDelete",id);
        Db.update(sqlPara);
    }

    public Record login(String mobile, String password)   {
        SqlPara sqlPara = Db.getSqlPara("user.findByMobile",mobile);
        Record r= Db.findFirst(sqlPara);
        if(null!=r) {
            doFilePath(r);
            if (StrKit.isBlank(r.get("token"))) {
                try {
                    RongCloudTool.registerUser(r);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            Random random = new Random();
            String tokenId = String.valueOf(random.nextLong());
            r.set("userToken", tokenId);
            String key = r.get("id");
            //缓存用户token
            RedisTool.setexObject(tokenId, StaticPublic.USERTOKENTIME,key);
            //缓存用户信息
            RedisTool.setexObject(key, StaticPublic.USERTOKENTIME, r);
        }
        return r;

    }
}
